package com.pl.lister;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.pl.entity.SyslogTb;
import com.pl.entity.TasklogTb;
import com.pl.service.SyslogTbService;
import com.pl.service.TasklogTbService;

import java.util.ArrayList;
import java.util.List;

/**
 * @version 1.0
 * @Author 小胤
 * @Date 2024/5/5 0:30
 * @注释
 */
public class TaskLogReadLister extends AnalysisEventListener<TasklogTb> {
    private static final int BATCH_COUNT=5;
    private TasklogTbService tasklogTbService;
    List<TasklogTb> list=new ArrayList<>();

    public TaskLogReadLister() {
    }

    public TaskLogReadLister(TasklogTbService tasklogTbService) {
        this.tasklogTbService = tasklogTbService;
    }

    @Override
    public void invoke(TasklogTb tasklogTb, AnalysisContext analysisContext) {

        list.add(tasklogTb);
        if (list.size()>=BATCH_COUNT){
            saveData();
            list.clear();
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        if (list.size()>0){
            saveData();
            list.clear();
        }
    }
    private void saveData() {
        System.out.println("开始批量导入");
        tasklogTbService.inserts(list);
        System.out.println("结束批量导入");
    }
}
